package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 15594
 */
public class InOutPutStream {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\15594\\Desktop\\测试\\welcome.jpg");
        File file2 = new File("C:\\Users\\15594\\Desktop\\测试\\sea.png");
        File file1 = new File("welcome.jpg");
        System.out.println(file.getAbsolutePath());
        System.out.println(file1.length());
        try {
            if (!file1.exists()){
                file1.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        input(file);
//        output(file,file1);
        output(file,file1,file2);

    }

    private static void output(File file, File file1, File file2) {
        FileInputStream input = null;
        FileInputStream input1 = null;
        FileOutputStream out = null;
        try {
            input  = new FileInputStream(file);
            input1  = new FileInputStream(file2);
            //是否追加写，ture为追加写，默认覆盖写
            out = new FileOutputStream(file1,true);
            byte[] bytes = new byte[46];
            int len = 0;
            while ((len = input1.read(bytes))!=-1){
                out.write(bytes,0,len);
            }
            while ((len = input.read(bytes))!=-1){
                out.write(bytes,0,len);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (input!=null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (input1!=null){
                try {
                    input1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }

    private static void output(File file, File file1) {
        FileInputStream input = null;
        FileOutputStream out = null;
        try {
            input  = new FileInputStream(file);
            out = new FileOutputStream(file1,false);
            byte[] bytes = new byte[46];
            int len = 0;
            while ((len = input.read(bytes))!=-1){
                out.write(bytes,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (input!=null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }

    private static void input(File file) {

    }


}
